// Waitlist modal functionality
function initWaitlistModal() {
    const waitlistBtn = document.getElementById('blog-waitlist-btn');
    const waitlistModal = document.getElementById('waitlist-modal');
    const closeModal = waitlistModal ? waitlistModal.querySelector('.close') : null;
    const waitlistForm = document.getElementById('blog-waitlist-form');
    
    // Open modal
    if (waitlistBtn && waitlistModal) {
        waitlistBtn.addEventListener('click', () => {
            waitlistModal.style.display = 'block';
            document.body.style.overflow = 'hidden';
        });
    }
    
    // Close modal
    if (closeModal && waitlistModal) {
        closeModal.addEventListener('click', () => {
            waitlistModal.style.display = 'none';
            document.body.style.overflow = 'auto';
        });
}

    // Close modal when clicking outside
    if (waitlistModal) {
        window.addEventListener('click', (e) => {
            if (e.target === waitlistModal) {
                waitlistModal.style.display = 'none';
                document.body.style.overflow = 'auto';
        }
    });
}

    // Handle form submission
    if (waitlistForm) {
        waitlistForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const emailInput = waitlistForm.querySelector('input[type="email"]');
            const email = emailInput.value;
            const submitBtn = waitlistForm.querySelector('button[type="submit"]');
            
            // Disable form during submission
            submitBtn.disabled = true;
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Submitting...';
            
            try {
                // Use Formspree service
                const response = await fetch('https://formspree.io/f/mqabrgep', {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ 
                        email: email,
                        message: 'New waitlist signup from blog page',
                        source: 'Blog Page'
                    })
                });
    
                if (response.ok) {
                    alert("Thanks – we'll be in touch!");
                    emailInput.value = '';
                    waitlistModal.style.display = 'none';
                    document.body.style.overflow = 'auto';
        } else {
                    throw new Error('Submission failed');
                }
            } catch (error) {
                console.error('Waitlist submission error:', error);
                alert('Submission failed, please try again later');
            } finally {
                // Re-enable form
                submitBtn.disabled = false;
                submitBtn.textContent = originalText;
        }
        });
    }
}

// Comment form functionality
function initCommentForm() {
    const commentForm = document.getElementById('comment-form');
    const successMessage = document.getElementById('success-message');
    
    if (commentForm) {
        commentForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const submitBtn = commentForm.querySelector('.submit-btn');
            const formData = new FormData(commentForm);
            
            // Get form values
            const name = formData.get('name');
            const email = formData.get('email');
            const comment = formData.get('comment');
            
            // Disable submit button
            submitBtn.disabled = true;
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Submitting...';
            
            try {
                // 使用Formspree服务提交评论
                // 如果想要单独的评论表单，请将下面的mqabrgep替换为新的表单ID
                const response = await fetch('https://formspree.io/f/xkgbdpjp', {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        name: name,
                        email: email,
                        message: comment,
                        source: 'ATP Blog Comments'
                    })
                });
                
                if (response.ok) {
                // Show success message
                successMessage.style.display = 'block';
                
                // Reset form
                commentForm.reset();
                
                    // Scroll to success message
                    successMessage.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    
                    // Hide success message after 5 seconds
                    setTimeout(() => {
                        successMessage.style.display = 'none';
                    }, 5000);
                } else {
                    throw new Error('Submission failed');
                }
            } catch (error) {
                console.error('Comment submission error:', error);
                alert('Submission failed, please try again later');
            } finally {
                // Re-enable submit button
                submitBtn.disabled = false;
                submitBtn.textContent = originalText;
            }
        });
    }
}

// Blog hover effects
function initBlogHoverEffects() {
    const blogPosts = document.querySelectorAll('.blog-post');
    
    blogPosts.forEach(post => {
        post.addEventListener('mouseenter', () => {
            post.style.transform = 'translateY(-8px)';
            });
            
        post.addEventListener('mouseleave', () => {
            post.style.transform = 'translateY(-4px)';
            });
        });
}

// Smooth scrolling for navigation links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        const target = document.querySelector(this.getAttribute('href'));
        if (target) {
            target.scrollIntoView({
                behavior: 'smooth',
                block: 'start'
            });
        }
    });
});

// Escape key to close modal
document.addEventListener('keydown', (e) => {
    const waitlistModal = document.getElementById('waitlist-modal');
    if (e.key === 'Escape' && waitlistModal && waitlistModal.style.display === 'block') {
        waitlistModal.style.display = 'none';
        document.body.style.overflow = 'auto';
    }
});

// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
    initWaitlistModal();
    initCommentForm();
    initBlogHoverEffects();
    
    // Add fade-in animation trigger
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.style.animationPlayState = 'running';
            }
        });
    }, {
        threshold: 0.1
    });
    
    // Observe blog posts for animation
    const blogPostsForAnimation = document.querySelectorAll('.blog-post');
    blogPostsForAnimation.forEach(post => {
        observer.observe(post);
    });
}); 